var util = require('../../../libs/util.js');
var app = getApp();
Page({
  data: {
    id: '',
    name: '',
    remark:'',
    photo: "/images/upload-add.svg",
    stockType_text: ["入库", "出库"],
    stockType: 0,
    warehouse_text: [],
    warehouse_code:[],
    warehouse: 0,
    sub_warehouse_text: [],
    sub_warehouse_code: [],
    sub_warehouse: 0,
    goods_text: [],
    goods_code: [],
    goods: 0,
    supply_text: [],
    supply_code: [],
    supply: 0,
    operDate: util.dateObjectformat(new Date(), 'yyyy-MM-dd')
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--出入库单" });
    this.setData({
      id: e.warehouse
    });
    wx.showToast({
      title: "数据加载中...",
      icon: 'loading',
      duration: 3000
    });
    this.loadData();
  },
  loadData: function () {
    var that = this;
    wx.request({
      url: app.server + '/wechat/wxapplet/getStockPicker',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        openid: app.globalData.openid
      },
      success: function (res) {
        wx.hideToast();
        console.log(res.data)
        var wh_data = res.data.data[0] || [], whl_data = res.data.data[1] || [], goods_data = res.data.data[2] || [], supply_data = res.data.data[3] || [];
        var warehouse_text = [],
          warehouse_code = []; 
        for (var i = 0; i < wh_data.length; i++) {
          warehouse_text.push(wh_data[i].name);
          warehouse_code.push(wh_data[i].ID);
          if (wh_data[i].ID == that.data.id){
            that.setData({
              warehouse: i
            });
          }
        };
        that.setData({
          warehouse_text: warehouse_text,
          warehouse_code: warehouse_code
        });
        var sub_warehouse_text = [],
          sub_warehouse_code = [];
        for (var i = 0; i < whl_data.length; i++) {
          sub_warehouse_text.push(whl_data[i].name);
          sub_warehouse_code.push(whl_data[i].ID);
        };
        that.setData({
          sub_warehouse_text: sub_warehouse_text,
          sub_warehouse_code: sub_warehouse_code
        });
        var goods_text = [],
          goods_code = [];
        for (var i = 0; i < goods_data.length; i++) {
          goods_text.push(goods_data[i].name);
          goods_code.push(goods_data[i].ID);
        };
        that.setData({
          goods_text: goods_text,
          goods_code: goods_code
        });
        var supply_text = [],
          supply_code = [];
        for (var i = 0; i < supply_data.length; i++) {
          supply_text.push(supply_data[i].name);
          supply_code.push(supply_data[i].ID);
        };
        that.setData({
          supply_text: supply_text,
          supply_code: supply_code
        });
      },
      fail: function (error) {
        wx.hideToast();
      }
    })
  },
  onReady: function () {
    wx.hideToast()
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  bindPickerChange: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var item = String(e.currentTarget.id), item_value = {};
        item_value[item] = res.tempFilePaths[0];
        that.setData(item_value);
        wx.uploadFile({
          url: 'https://www.uamaker.com/wechat/wxapplet/uploadPicture',
          header:{
            'Authorization': app.globalData.token
          },
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'type': item
          },
          success: function (res) {
            console.log(res)
            var data = JSON.parse(res.data);
            if (data.success) {
              var upload_item = String(e.currentTarget.id), item_value = {};
              item_value[upload_item] = app.server + data.data[0].path;
              that.setData(item_value);
            }
          }
        })
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  confirmData: function () {
    var that = this;
    wx.showLoading({
      title: '请稍候...',
    });
    if (that.data.count == '' || that.data.count == undefined) {
      wx.showToast({
        title: "请输入数量",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    util.postData("stock", {
      stockType: that.data.stockType_text[that.data.stockType],
      warehouse: that.data.warehouse_code[that.data.warehouse],
      sub_warehouse: that.data.sub_warehouse_code[that.data.sub_warehouse],
      goods: that.data.goods_code[that.data.goods],
      count: that.data.count,
      supply: that.data.supply_code[that.data.supply],
      operDate: that.data.operDate,
      photo: that.data.photo,
      remark: that.data.remark
    }, function (res, error) {
      //console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 2000
        });
        return false;
      }
      if (res.data.success) {
        wx.showLoading({
          title: "提交成功",
          icon: 'success',
          success: function () {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/warehouse/stock'
              })
            }, 2000);
          }
        });
      } else {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 2000
        });
      }
    });
  },
  cancelConfirmData: function () {
    wx.redirectTo({
      url: '/pages/warehouse/stock'
    })
  }
});